/*
 * 74150.c
 *
 *  Created on: Aug 20, 2009
 *      Author: Orlando Arias
 *     License: GPLv3
 */

#include <inttypes.h>
#include "WProgram.h"

#include "74150.h"

void mx74150_init(Muxer74150 *m){
	for (int i = 0; i<4; i++)
		pinMode(m->select[i], OUTPUT);

	pinMode(m->strobe, OUTPUT);
	pinMode(m->output, INPUT);
}

uint8_t mx74150_getPin(Muxer74150 *m, uint8_t pin){
	for (int i = 0; i<4; i++)
		digitalWrite(m->select[i], ((pin>>i) & 0x01) ? HIGH:LOW);

	// As per datasheet, this returns !A
	return digitalRead(m->output);
}

void mx74150_setStrobe(Muxer74150 *m, uint8_t mode){
	if (!((mode == HIGH) || (mode == LOW))) return;
	digitalWrite(m->strobe, mode);
}

